package top.yonyong.mynacos.configcenter.container;

import lombok.Builder;
import lombok.Data;
import top.yonyong.mynacos.configcenter.pojo.ConfigInfo;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Describtion put key-value config
 * @Author yonyong
 * @Date 2020/7/6 9:31
 * @Version 1.0.0
 **/
@Data
@Builder(toBuilder = true)
public class MyConfigContainer {

    /**
     * key-val map
     */
    private List<ConfigInfo> vals;

    /**
     * get config
     * @param key
     * @return
     */
    public Object get(String key){
        final List<ConfigInfo> collect = vals.stream().filter(configInfo -> configInfo.getKey().equals(key)).collect(Collectors.toList());
        return collect;
    }

    /**
     * set config
     * @param key
     * @param val
     * @return
     */
    public Boolean set(String key,String val){
        ConfigInfo configInfo = ConfigInfo.builder()
                .key(key)
                .val(val)
                .group("DEFAULT")
                .handler("YONYONG")
                .createTime(new Date())
                .build();
        vals.add(configInfo);
        return true;
    }
}
